package com.manolo.client.form;


import javax.swing.JOptionPane;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import java.awt.Point;
import java.awt.Dimension;

import javax.swing.JButton;

import com.manolo.client.connection.ClientMultiplayer;
import com.manolo.client.entity.Player;
/**
 * Example of components laid out in a grid
 */
public class ConnectionWindow extends javax.swing.JFrame {
	private ClientMultiplayer client;
	private Player player;
	private javax.swing.JPanel ivjJFrameContentPane = null;
	private JLabel jlblServer = null;
	private JLabel jlblPort = null;
	private JTextField jtxtServer = null;
	private JTextField jtxtPort = null;
	private JButton jbtnConect = null;
	private JButton jbtnCancel = null;
	private JLabel jlblName = null;
	private JTextField jtxtPort1 = null;
	
	public static void main(String args[]) throws java.io.IOException
	{
		ConnectionWindow window = new ConnectionWindow();
		window.show();
	}
	public ConnectionWindow() {
		super();
		initialize();
	}

	/**
	 * Return the JFrameContentPane property value.
	 * @return javax.swing.JPanel
	 */
	public javax.swing.JPanel getJFrameContentPane() {
		if (ivjJFrameContentPane == null) {
			jlblName = new JLabel();
			jlblName.setText("Name:");
			jlblName.setSize(new Dimension(66, 22));
			jlblName.setLocation(new Point(15, 110));
			jlblPort = new JLabel();
			jlblPort.setText("Port: ");
			jlblPort.setSize(new Dimension(66, 22));
			jlblPort.setLocation(new Point(15, 70));
			jlblServer = new JLabel();
			jlblServer.setText("Server: ");
			jlblServer.setLocation(new Point(15, 30));
			jlblServer.setSize(new Dimension(66, 22));
			ivjJFrameContentPane = new javax.swing.JPanel();
			ivjJFrameContentPane.setName("JFrameContentPane");
			ivjJFrameContentPane.setLayout(null);
			ivjJFrameContentPane.add(jlblServer, null);
			ivjJFrameContentPane.add(jlblPort, null);
			ivjJFrameContentPane.add(getJtxtServer(), null);
			ivjJFrameContentPane.add(getJtxtPort(), null);
			ivjJFrameContentPane.add(getJbtnConect(), null);
			ivjJFrameContentPane.add(getJbtnCancel(), null);
			ivjJFrameContentPane.add(jlblName, null);
			ivjJFrameContentPane.add(getJtxtPort1(), null);
		}
		return ivjJFrameContentPane;
	}

	/**
	 * Initialize the class.
	 */
	public void initialize() {

		this.setName("JFrame1");
		this.setResizable(false);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setBounds(45, 25, 317, 273);
		this.setTitle("Truco Manolo");
		this.setContentPane(getJFrameContentPane());

	}

	/**
	 * This method initializes jtxtServer	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxtServer() {
		if (jtxtServer == null) {
			jtxtServer = new JTextField();
			jtxtServer.setText("localhost");
			jtxtServer.setLocation(new Point(94, 30));
			jtxtServer.setSize(new Dimension(191, 27));
		}
		return jtxtServer;
	}

	/**
	 * This method initializes jtxtPort	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxtPort() {
		if (jtxtPort == null) {
			jtxtPort = new JTextField();
			jtxtPort.setLocation(new Point(94, 70));
			jtxtPort.setText("4555");
			jtxtPort.setSize(new Dimension(58, 27));
		}
		return jtxtPort;
	}

	/**
	 * This method initializes jbtnConect	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnConect() {
		if (jbtnConect == null) {
			jbtnConect = new JButton();
			jbtnConect.setBounds(new Rectangle(179, 195, 114, 36));
			jbtnConect.setText("Connect");
			jbtnConect.addMouseListener(new java.awt.event.MouseListener() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						client = new ClientMultiplayer(jtxtServer.getText(),Integer.parseInt(jtxtPort.getText()), player);
						Thread playerThread = new Thread(client);
						playerThread.start();
						
						MessageWindow msgWindow = new MessageWindow();
						msgWindow.showWindow(client.getPlayer());
					}
					catch(Exception ex)
					{
						JOptionPane.showMessageDialog(null,"Fail to connect to the server. Error: " + ex.getMessage());
					}
				}
				public void mousePressed(java.awt.event.MouseEvent e) {
				}
				public void mouseReleased(java.awt.event.MouseEvent e) {
				}
				public void mouseEntered(java.awt.event.MouseEvent e) {
				}
				public void mouseExited(java.awt.event.MouseEvent e) {
				}
			});
		}
		return jbtnConect;
	}

	/**
	 * This method initializes jbtnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJbtnCancel() {
		if (jbtnCancel == null) {
			jbtnCancel = new JButton();
			jbtnCancel.setLocation(new Point(47, 195));
			jbtnCancel.setText("Cancel");
			jbtnCancel.setSize(new Dimension(114, 36));
			jbtnCancel.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					System.exit(0);
				}
			});
			
		}
		return jbtnCancel;
	}

	/**
	 * This method initializes jtxtPort1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJtxtPort1() {
		if (jtxtPort1 == null) {
			jtxtPort1 = new JTextField();
			jtxtPort1.setText("");
			jtxtPort1.setSize(new Dimension(117, 27));
			jtxtPort1.setLocation(new Point(94, 110));
		}
		return jtxtPort1;
	}
}
